#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import pandas as pd
import tensorflow as tf
import numpy as np

# 开启info日志
tf.logging.set_verbosity(tf.logging.INFO)

# 训练集文件和测试集文件
TRAINING_DATA = "9783to6/train_8000.csv"
TEST_DATA = "9783to6/test_1783.csv"

COLUMNS = ["vital_capacity", "m50_run", "sit_and_reach", "jump_rope", "sit_up", "m50_return_run", "Cluster"]
FEATURES = ["vital_capacity", "m50_run", "sit_and_reach", "jump_rope", "sit_up", "m50_return_run"]
LABEL = "Cluster"


def my_input_fn(data_set):
    feature_cols = {k: tf.constant(data_set[k].values) for k in FEATURES}
    labels = tf.constant(data_set[LABEL].values)
    return feature_cols, labels


# 加载训练集和测试集
training_set = pd.read_csv(TRAINING_DATA, skipinitialspace=True, skiprows=1, names=COLUMNS)
test_set = pd.read_csv(TEST_DATA, skipinitialspace=True, skiprows=1, names=COLUMNS)

# Specify that all features have real-value data
feature_columns = [tf.contrib.layers.real_valued_column(k) for k in FEATURES]

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                            hidden_units=[10],
                                            n_classes=6,
                                            model_dir="my_model_input")

# Fit model.
classifier.fit(input_fn=lambda: my_input_fn(training_set), steps=200)

# Evaluate accuracy.
evaluate = classifier.evaluate(input_fn=lambda: my_input_fn(test_set))
# print(evaluate)
accuracy_score = evaluate["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))

# Classify two new flower samples.
new_samples = np.array(
    [[2211, 9.8, 9.7, 74, 24, 116], [1939, 9.8, 9.3, 106, 24, 107], [1503, 9.5, 10.7, 119, 44, 117]], dtype=float)
y = list(classifier.predict(new_samples, as_iterable=True))
print('Predictions: {}'.format(str(y)))


# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
#
# import itertools
#
# import pandas as pd
# import tensorflow as tf
#
# tf.logging.set_verbosity(tf.logging.INFO)
#
# COLUMNS = ["crim", "zn", "indus", "nox", "rm", "age",
#            "dis", "tax", "ptratio", "medv"]
# FEATURES = ["crim", "zn", "indus", "nox", "rm",
#             "age", "dis", "tax", "ptratio"]
# LABEL = "medv"
#
#
# def input_fn(data_set):
#     feature_cols = {k: tf.constant(data_set[k].values) for k in FEATURES}
#     labels = tf.constant(data_set[LABEL].values)
#     return feature_cols, labels
#
#
# def main(unused_argv):
#     # Load datasets
#     training_set = pd.read_csv("/Users/David/Desktop/boston_train.csv", skipinitialspace=True,
#                                skiprows=1, names=COLUMNS)
#     test_set = pd.read_csv("/Users/David/Desktop/boston_test.csv", skipinitialspace=True,
#                            skiprows=1, names=COLUMNS)
#
#     # Set of 6 examples for which to predict median house values
#     # prediction_set = pd.read_csv("boston_predict.csv", skipinitialspace=True,
#     #                              skiprows=1, names=COLUMNS)
#
#     # Feature cols
#     feature_cols = [tf.contrib.layers.real_valued_column(k)
#                     for k in FEATURES]
#
#     # Build 2 layer fully connected DNN with 10, 10 units respectively.
#     regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
#                                               hidden_units=[10, 10],
#                                               model_dir="boston_model")
#
#     # Fit
#     regressor.fit(input_fn=lambda: input_fn(training_set), steps=5000)
#
#     # Score accuracy
#     ev = regressor.evaluate(input_fn=lambda: input_fn(test_set), steps=1)
#     loss_score = ev["loss"]
#     print("Loss: {0:f}".format(loss_score))
#
#     # # Print out predictions
#     # y = regressor.predict(input_fn=lambda: input_fn(prediction_set))
#     # # .predict() returns an iterator; convert to a list and print predictions
#     # predictions = list(itertools.islice(y, 6))
#     # print("Predictions: {}".format(str(predictions)))
#
#
# if __name__ == "__main__":
#     tf.app.run()
